﻿Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Imports System.ComponentModel
Partial Public Class frmAdminEditChargeTypes
    Private intSaveNeworEdit As Integer
    Public WriteOnly Property SaveNewOrEdit() As Integer
        Set(ByVal value As Integer)
            intSaveNeworEdit = value
        End Set
    End Property

    Private _IsTaxZero As Integer
    Public Property IsTaxZero() As Integer
        Get
            Return _IsTaxZero
        End Get
        Set(ByVal value As Integer)
            _IsTaxZero = value
        End Set
    End Property
    Private _ChargeTypeId As Integer
    Public Property ChargeTypeId() As Integer
        Get
            Return _ChargeTypeId
        End Get
        Set(ByVal value As Integer)
            _ChargeTypeId = value
        End Set
    End Property

    Public WriteOnly Property FormTitle() As String
        Set(ByVal value As String)
            Me.Title = value
        End Set
    End Property
    Private _Tax As Boolean
    Public Property Tax() As Boolean
        Get
            Return _Tax
        End Get
        Set(ByVal value As Boolean)
            _Tax = value
        End Set
    End Property

    Private _Button As String
    Public Property ButtonVisibile() As String
        Get
            Return _Button
        End Get
        Set(ByVal value As String)
            _Button = value
        End Set
    End Property

    Public ReadOnly Property TextChargeType() As TextBox
        Get
            Return Me.txtChargeType
        End Get
    End Property

    Public ReadOnly Property TextTax() As TextBox
        Get
            Return Me.txtTax
        End Get
    End Property
    Private category As Integer
    Public Property ChargeCategory() As Integer
        Get
            Return category
        End Get
        Set(ByVal value As Integer)
            category = value
        End Set
    End Property



    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub frmAdminEditChargeTypes_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        If Tax = True Then
            Me.radYes.IsChecked = True
            Me.radNo.IsChecked = False
        Else
            Me.radNo.IsChecked = True
            Me.radYes.IsChecked = False
        End If

        If Not Me.ButtonVisibile Is Nothing Then
            If ButtonVisibile.Trim.ToUpper = "NORMAL" Then
                Me.radNormal.IsChecked = True
            ElseIf ButtonVisibile.Trim.ToUpper = "SELECT" Then
                Me.radSelect.IsChecked = True
            End If
        End If

        With Me.drpCategory
            .SelectedValuePath = "ChargeCat_ID"
            .DisplayMemberPath = "ChargeCat_Name"
            .ItemsSource = CType(Admin.CategoryList, IListSource).GetList
            If Me.ChargeCategory = 0 Then
                .SelectedIndex = -1
                radNormal.IsChecked = True
            Else
                radSelect.IsChecked = True
                .SelectedValue = ChargeCategory
            End If
        End With
       
    End Sub

    Private Sub radSelect_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles radSelect.Checked
        Me.drpCategory.IsEnabled = True
    End Sub

    Private Sub radNormal_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles radNormal.Checked
        Me.drpCategory.IsEnabled = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        'insert into a_chargetypes(ChargeType_Name)

        If txtChargeType.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Please enter Charge Type.", CommonDialog.CustomDialogIcons.Information, "Enter")
            txtChargeType.Focus()
            Exit Sub
        Else
            btnSave.IsEnabled = False
            Dim Sql As New StringBuilder
            Select Case intSaveNeworEdit
                Case 1 'new one
                    Sql = New StringBuilder
                    If Me.radNormal.IsChecked = True Then
                        If Me.radYes.IsChecked = True Then
                            If txtTax.Text.Trim = String.Empty Then
                                CommonFuncs.DisplayMessage("Enter the Tax %", CommonDialog.CustomDialogIcons.Information, "Enter")
                                txtTax.Focus()
                                Me.btnSave.IsEnabled = True
                                Exit Sub
                            End If
                            With Sql
                                .Append("")
                                .Append("insert into a_chargetypes ")
                                .Append("  (ChargeType_Name,ChargeType_VAT,ChargeType_CategoryID)")
                                .Append(" Values(")
                                .Append(" '" & Me.txtChargeType.Text & "'," & txtTax.Text & "," & 0 & ")")
                            End With
                        ElseIf radNo.IsChecked = True Then
                            With Sql
                                .Append("")
                                .Append("insert into a_chargetypes ")
                                .Append("  (ChargeType_Name,ChargeType_VAT,ChargeType_CategoryID)")
                                .Append(" Values(")
                                .Append(" '" & Me.txtChargeType.Text & "'," & 0 & "," & 0 & ")")
                            End With
                        End If

                    ElseIf Me.radSelect.IsChecked = True Then
                        Dim selectedCategeroy As Integer = 0
                        If drpCategory.SelectedIndex = -1 Then
                            selectedCategeroy = 0
                        Else
                            selectedCategeroy = drpCategory.SelectedValue
                        End If
                        If Me.radYes.IsChecked = True Then
                            If txtTax.Text.Trim = String.Empty Then
                                CommonFuncs.DisplayMessage("Enter the Tax %", CommonDialog.CustomDialogIcons.Information, "Tax")
                                txtTax.Focus()
                                Me.btnSave.IsEnabled = True
                                Exit Sub
                            End If
                            With Sql
                                .Append("")
                                .Append("insert into a_chargetypes ")
                                .Append("  (ChargeType_Name,,ChargeType_VAT,ChargeType_CategoryID)")
                                .Append(" Values(")
                                .Append(" '" & Me.txtChargeType.Text & "'," & txtTax.Text & "," & selectedCategeroy & ")")
                            End With
                        ElseIf radNo.IsChecked = True Then
                            With Sql
                                .Append("")
                                .Append("insert into a_chargetypes ")
                                .Append("  (ChargeType_Name,ChargeType_VAT,ChargeType_CategoryID)")
                                .Append(" Values(")
                                .Append(" '" & Me.txtChargeType.Text & "'," & 0 & "," & selectedCategeroy)
                                .Append(")")
                            End With
                        End If

                    End If


                    If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then

                        CommonFuncs.DisplayMessage("The Charge Type has been added successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                        Me.Close()
                        'Exit Sub
                    Else
                        CommonFuncs.DisplayMessage("There is an error adding the Charge Type to the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                        txtChargeType.Focus()
                        btnSave.IsEnabled = True
                        'Exit Sub
                    End If
                Case 2 'existing one
                    Sql = New StringBuilder
                    Dim selectedCategeroy As Integer = 0
                    If drpCategory.SelectedIndex = -1 Then
                        selectedCategeroy = 0
                    Else
                        selectedCategeroy = drpCategory.SelectedValue
                    End If
                    If Me.radYes.IsChecked = True Then
                        If txtTax.Text.Trim = String.Empty Then
                            CommonFuncs.DisplayMessage("Enter the Tax %", CommonDialog.CustomDialogIcons.Information, "")
                            txtTax.Focus()
                            Me.btnSave.IsEnabled = True
                            Exit Sub
                        End If
                        ' check value Me.IsTaxZero 
                        ' and then update the data 
                        With Sql
                            .Append("")
                            .Append("update  a_chargetypes set  ChargeType_Name='" & Me.txtChargeType.Text & "', ")
                            .Append("  ChargeType_CategoryID=" & selectedCategeroy)
                            .Append("  ,ChargeType_VAT=" & txtTax.Text & " where ChargeType_ID=" & ChargeTypeId)
                        End With

                    ElseIf radNo.IsChecked = True Then
                        With Sql
                            .Append("")
                            .Append("update  a_chargetypes set  ChargeType_Name='" & Me.txtChargeType.Text & "', ")
                            .Append("  ChargeType_CategoryID=" & selectedCategeroy)
                            .Append("  ,ChargeType_VAT=" & 0 & " where ChargeType_ID=" & ChargeTypeId)
                        End With
                    End If

                    If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                        'refreshList()
                        CommonFuncs.DisplayMessage("The Charge Type has been updated successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                        Me.Close()
                        'Exit Sub
                    Else
                        CommonFuncs.DisplayMessage("There is an error in editing the Charge Type details. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                        txtChargeType.Focus()
                        btnSave.IsEnabled = True
                        'Exit Sub
                    End If

            End Select



        End If
    End Sub

    
    Private Sub radNo_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radNo.Checked
        Me.lblTax.Visibility = Windows.Visibility.Hidden
        Me.txtTax.Visibility = Windows.Visibility.Hidden
    End Sub

    Private Sub radYes_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radYes.Checked
        Me.lblTax.Visibility = Windows.Visibility.Visible
        Me.txtTax.Visibility = Windows.Visibility.Visible
        lblTax.Focus()
    End Sub
End Class
